<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct()
    {
        parent::__construct();
            // Your own constructor code
            
        if(!$this->session->userdata('logged_in') or $this->session->userdata('type') != '0')
        {
            redirect('user/login');
        }
               
    } 
    
	public function index()
	{

 	         $data['subview'] = '/admin/view_admin_main';
	         $this->load->view('layouts/default1',$data);
        
	}
    public function brands()
    {
            $data['brands'] = $this->config_model->get_brands()->result();
 	         $data['subview'] = '/admin/view_brands';
	         $this->load->view('layouts/default1',$data);         
        
    }
    public function brand_add()
    {
        $post_info = array(
            'name'  =>  $this->input->post('brand_name')
        );
        $this->db->insert('brands',$post_info);
        redirect('admin/brands');
    }
    public function brand_update()
    {
        if($this->input->post('change'))
        {
            $post_info = array(
                'name' => $this->input->post('brand_name')
            );

            $this->db->where('brand_id', $this->input->post('brand_id'));
            $this->db->update('brands', $post_info);
            redirect('admin/brands');
        }
        else if($this->input->post('delete'))
        {
            $this->db->delete('brands', array('brand_id' => $this->input->post('brand_id'))); 
             redirect('admin/brands');
        }
    }
    public function subcategories($category_id = NULL)
    {
        if($category_id != NULL or $this->input->post('select'))
        {
             if($this->input->post('select'))
             {
                $category_id = $this->input->post('category_id');
             }
             $data['category_id'] = $category_id;
             $data['subcategories'] = $this->config_model->get_subcategories($category_id)->result();
             $data['categories'] = $this->config_model->get_categories()->result();
 	         $data['subview'] = '/admin/view_subcategories';
	         $this->load->view('layouts/default1',$data); 
        }
        else
        {
             $data['categories'] = $this->config_model->get_categories()->result();
 	         $data['subview'] = '/admin/view_subcategories';
	         $this->load->view('layouts/default1',$data); 
        }
    }
    public function categories()
    {
            $data['categories'] = $this->config_model->get_categories()->result();
 	         $data['subview'] = '/admin/view_categories';
	         $this->load->view('layouts/default1',$data);        
    }
    public function category_add()
    {
        $post_info = array(
            'name'  =>  $this->input->post('category_name')
        );
        $this->db->insert('categories',$post_info);
        redirect('admin/categories');
    }
    public function category_update()
    {
        if($this->input->post('change'))
        {
            $post_info = array(
                'name' => $this->input->post('category_name')
            );

            $this->db->where('category_id', $this->input->post('category_id'));
            $this->db->update('categories', $post_info);
            redirect('admin/categories');
        }
        else if($this->input->post('delete'))
        {
            $this->db->delete('categories', array('category_id' => $this->input->post('category_id'))); 
             redirect('admin/categories');
        }
    }
    public function subcategory_add()
    {
        $post_info = array(
            'name'  =>  $this->input->post('subcategory_name'),
            'category_id'   =>  $this->input->post('category_id')
        );
        $this->db->insert('subcategories',$post_info);
        redirect('admin/subcategories/'.$this->input->post('category_id'));
    }
    
    
    public function subcategory_update()
    {
        if($this->input->post('change'))
        {
            $post_info = array(
                'name' => $this->input->post('subcategory_name')
                
            );
         

            $this->db->where('subcategories_id',$this->input->post('subcategory_id'));
            $this->db->update('subcategories', $post_info);
            redirect('admin/subcategories/'.$this->input->post('category_id'));
        }
        else if($this->input->post('delete'))
        {
            $this->db->delete('subcategories', array('subcategories_id' => $this->input->post('subcategory_id'))); 
             redirect('admin/subcategories/'.$this->input->post('category_id'));
        }
    }    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */